package com.tjp.eight.ui.fragment.detailjb;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.tjp.eight.R;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.utils.SharePreferencesUtils;
import com.tjp.eight.views.rcview.EndlessRecyclerOnScrollListener;
import com.tjp.eight.views.rcview.HeaderViewRecyclerAdapter;

/**
 * Created by Administrator on 2017/4/10.
 */
public class FragmentDetailJB extends BaseFragment<DetailJBPresenter> implements DetailJBView {
    private RecyclerView rv_detail_jb;
    private HeaderViewRecyclerAdapter headerViewRecyclerAdapter;
    private DetailJBAdapter detailJBAdapter;
    private View footView;
    private int page = 1;

    @Override
    protected DetailJBPresenter createPresenter() {
        return new DetailJBPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);

        if (contentView == null) {
            contentView = inflater.inflate(R.layout.fragment_detail_jb, container, false);
        }
        initViews();
        return contentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        presenter.loadData(getPJson());
    }

    void initViews() {
        rv_detail_jb = (RecyclerView) contentView.findViewById(R.id.rv_detail_jb);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false);
        rv_detail_jb.setLayoutManager(linearLayoutManager);
        detailJBAdapter = new DetailJBAdapter(getContext());
        headerViewRecyclerAdapter = new HeaderViewRecyclerAdapter(detailJBAdapter);
        footView = LayoutInflater.from(getContext()).inflate(R.layout.swipe_refresh_footer, rv_detail_jb, false);
        headerViewRecyclerAdapter.addFooterView(footView);
        rv_detail_jb.setAdapter(headerViewRecyclerAdapter);
        rv_detail_jb.addOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) {
            @Override
            public void onLoadMore(int currentPage) {
                page = currentPage;
                presenter.loadMdata(getPJson());
            }
        });

    }

    private String getPJson() {
        JBDetailPModel model = new JBDetailPModel();
        model.uid = SharePreferencesUtils.getUid();
        model.loginkey = SharePreferencesUtils.getLoginKey();
        model.page = page;
        return new Gson().toJson(model);
    }

    @Override
    public void loadMSuccess(DetailJBRModel model) {
        if (model != null && model.data != null) {
            detailJBAdapter.addData(model.data.list);
            if (detailJBAdapter.getItemCount() >= model.data.total_items) {
                headerViewRecyclerAdapter.removeFootByIndex(0);
            } else {
                if (headerViewRecyclerAdapter.getFooterCount() == 0) {
                    headerViewRecyclerAdapter.addFooterView(footView);
                }
            }
        }
    }

    @Override
    public void loadSuccess(DetailJBRModel model) {
        if (model != null && model.data != null) {
            detailJBAdapter.addData(model.data.list);
            if (detailJBAdapter.getItemCount() >= model.data.total_items) {
                headerViewRecyclerAdapter.removeFootByIndex(0);
            } else {
                if (headerViewRecyclerAdapter.getFooterCount() == 0) {
                    headerViewRecyclerAdapter.addFooterView(footView);
                }
            }
        }
    }

    @Override
    public void fail(String msg) {

    }
}
